{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bike-Sharing-Dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1导入必要的工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 数据探索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>dteday</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2011-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>331</td>\n",
       "      <td>654</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2011-01-02</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>131</td>\n",
       "      <td>670</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2011-01-03</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>120</td>\n",
       "      <td>1229</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2011-01-04</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>108</td>\n",
       "      <td>1454</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2011-01-05</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>82</td>\n",
       "      <td>1518</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant     dteday  season  yr  mnth  holiday  weekday  workingday  \\\n",
       "0        1 2011-01-01       1   0     1        0        6           0   \n",
       "1        2 2011-01-02       1   0     1        0        0           0   \n",
       "2        3 2011-01-03       1   0     1        0        1           1   \n",
       "3        4 2011-01-04       1   0     1        0        2           1   \n",
       "4        5 2011-01-05       1   0     1        0        3           1   \n",
       "\n",
       "   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n",
       "0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n",
       "1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n",
       "2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n",
       "3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n",
       "4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n",
       "\n",
       "    cnt  \n",
       "0   985  \n",
       "1   801  \n",
       "2  1349  \n",
       "3  1562  \n",
       "4  1600  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"/Users/admin/Desktop/Bike-Sharing-Dataset/day.csv\", parse_dates=['dteday'])\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  2.2 数据基本信息\n",
    "样本数目、特征维数\n",
    "每个特征的类型、空值样本的数目、数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(731, 16)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 数据探索\n",
    "\n",
    "对数据的探索有助于我们在第三步中根据数据的特点选择合适的模型类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['instant', 'dteday', 'season', 'yr', 'mnth', 'holiday', 'weekday',\n",
       "       'workingday', 'weathersit', 'temp', 'atemp', 'hum', 'windspeed',\n",
       "       'casual', 'registered', 'cnt'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['season', 'yr', 'mnth', 'holiday', 'weekday', 'weathersit', 'temp',\n",
       "       'hum', 'windspeed', 'cnt'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 移除无用的特征   \n",
    "remove_columns = ['instant', 'atemp', 'dteday', 'workingday', 'casual', 'registered']   \n",
    "data = data.drop(remove_columns, axis=1)\n",
    "data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1导入数据\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练数据和测试数据分割(请将 2012 年的数据作为测试数据)，(0: 2011, 1:2012)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/admin/Documents/Anaconda5.2/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_split.py:2026: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.\n",
      "  FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train_data0, test_data0 = train_test_split(data[data['yr'] == 0], train_size=0.75)\n",
    "train_data1, test_data1 = train_test_split(data[data['yr'] == 1], train_size=0.75)\n",
    "\n",
    "y_train_data = np.hstack([train_data0['cnt'].values, train_data1['cnt'].values])\n",
    "y_test_data = np.hstack([test_data0['cnt'].values, test_data1['cnt'].values])\n",
    "\n",
    "X_train_data = np.vstack([train_data0.drop('cnt', axis=1).values, train_data1.drop('cnt', axis=1).values])\n",
    "X_test_data = np.vstack([test_data0.drop('cnt', axis=1).values, test_data1.drop('cnt', axis=1).values])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train_data = data[data['yr'] == 0]\n",
    "# test_data = data[data['yr'] == 1]\n",
    "\n",
    "# y_train_data = train_data['cnt']\n",
    "# X_train_data = train_data.drop('cnt', axis=1)\n",
    "\n",
    "# y_test_data = test_data['cnt']\n",
    "# X_test_data = test_data.drop('cnt', axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Unique values of season: \n",
      "[1 2 3 4]\n",
      "Unique values of mnth: \n",
      "[ 1  2  3  4  5  6  7  8  9 10 11 12]\n",
      "Unique values of holiday: \n",
      "[0 1]\n",
      "Unique values of weekday: \n",
      "[6 0 1 2 3 4 5]\n",
      "Unique values of weathersit: \n",
      "[2 1 3]\n",
      "Unique values of yr: \n",
      "[0 1]\n"
     ]
    }
   ],
   "source": [
    "feats = ['season', 'mnth', 'holiday', 'weekday', 'weathersit', 'yr']\n",
    "# 分类变量\n",
    "for cat in feats:\n",
    "    print(f'Unique values of {cat}: ')\n",
    "    print(data[cat].unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 one-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sklearn.preprocessing import OneHotEncoder\n",
    "# from sklearn.linear_model import LinearRegression\n",
    "# from scipy import sparse\n",
    "\n",
    "# enc = OneHotEncoder()\n",
    "# traindata2 = X_train_data\n",
    "\n",
    "# # x_train1 = enc.fit_transform(traindata2['season'].values.reshape(-1, 1))\n",
    "# # x_train2 = enc.fit_transform(traindata2['mnth'].values.reshape(-1, 1))\n",
    "# # x_train3 = enc.fit_transform(traindata2['holiday'].values.reshape(-1, 1))\n",
    "# # x_train4 = enc.fit_transform(traindata2['weekday'].values.reshape(-1, 1))\n",
    "# # x_train5 = enc.fit_transform(traindata2['weathersit'].values.reshape(-1, 1))\n",
    "# # X_train15 = sparse.hstack((x_train1, x_train2, x_train3, x_train4, x_train5))\n",
    "# # X_train15.shape\n",
    "\n",
    "# x_train1 = enc.fit_transform(traindata2[:, 0].reshape(-1, 1)) #season\n",
    "# x_train2 = enc.fit_transform(traindata2[:, 2].reshape(-1, 1)) #mnth\n",
    "# x_train3 = enc.fit_transform(traindata2[:, 3].reshape(-1, 1)) #holiday\n",
    "# x_train4 = enc.fit_transform(traindata2[:, 4].reshape(-1, 1)) #weekday\n",
    "# x_train5 = enc.fit_transform(traindata2[:, 5].reshape(-1, 1)) #weathersit\n",
    "# X_train15 = sparse.hstack((x_train1, x_train2, x_train3, x_train4, x_train5))\n",
    "# X_train15.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<547x28 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 2735 stored elements in COOrdinate format>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from scipy import sparse\n",
    "\n",
    "# feats = ['season', 'mnth', 'holiday', 'weekday', 'weathersit']\n",
    "# enc = OneHotEncoder()\n",
    "# traindata2 = X_train_data\n",
    "# testdata2 = X_test_data\n",
    "# for i, feat in enumerate(feats):\n",
    "#     x_train_tmp = enc.fit_transform(traindata2[feat].values.reshape(-1, 1))\n",
    "#     x_test_tmp = enc.fit_transform(testdata2[feat].values.reshape(-1, 1))\n",
    "\n",
    "#     if i == 0:\n",
    "#         X_train_t, X_test_t = x_train_tmp, x_test_tmp\n",
    "#     else:\n",
    "#         X_train_t, X_test_t = sparse.hstack((X_train_t, x_train_tmp)), sparse.hstack((X_test_t, x_test_tmp))\n",
    "\n",
    "feats = [0, 2, 3, 4, 5]\n",
    "enc = OneHotEncoder()\n",
    "traindata2 = X_train_data\n",
    "testdata2 = X_test_data\n",
    "for i, feat in enumerate(feats):\n",
    "    x_train_tmp = enc.fit_transform(traindata2[:, feat].reshape(-1, 1))\n",
    "    x_test_tmp = enc.fit_transform(testdata2[:, feat].reshape(-1, 1))\n",
    "\n",
    "    if i == 0:\n",
    "        X_train_t, X_test_t = x_train_tmp, x_test_tmp\n",
    "    else:\n",
    "        X_train_t, X_test_t = sparse.hstack((X_train_t, x_train_tmp)), sparse.hstack((X_test_t, x_test_tmp))\n",
    "X_train_t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(184, 3)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "ss_X = StandardScaler()\n",
    "traindata3 = X_train_data\n",
    "testdata3 = X_test_data\n",
    "\n",
    "# x_train6 = ss_X.fit_transform(traindata3['temp'].values.reshape(-1, 1))\n",
    "# x_train7 = ss_X.fit_transform(traindata3['hum'].values.reshape(-1, 1))\n",
    "# x_train8 = ss_X.fit_transform(traindata3['windspeed'].values.reshape(-1, 1))\n",
    "# X_train68 = np.hstack((x_train6, x_train7, x_train8))\n",
    "\n",
    "# feats = [\"temp\", \"hum\", \"windspeed\"]\n",
    "# x_train68, x_test68 = ss_X.fit_transform(traindata3[feats].values), ss_X.fit_transform(testdata3[feats].values)\n",
    "\n",
    "\n",
    "feats = [\"temp\", \"hum\", \"windspeed\"]  #'casual', 'registered'\n",
    "x_train68, x_test68 = ss_X.fit_transform(traindata3[:, 6:11]), ss_X.fit_transform(testdata3[:, 6:11])\n",
    "x_test68.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<184x31 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 1472 stored elements in COOrdinate format>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 合并\n",
    "X_train, X_test = sparse.hstack((X_train_t, x_train68)), sparse.hstack((X_test_t, x_test68))\n",
    "X_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/admin/Documents/Anaconda5.2/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n",
      "  warnings.warn(msg, DataConversionWarning)\n",
      "/Users/admin/Documents/Anaconda5.2/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n",
      "  warnings.warn(msg, DataConversionWarning)\n",
      "/Users/admin/Documents/Anaconda5.2/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n",
      "  warnings.warn(msg, DataConversionWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[-0.15898618],\n",
       "       [ 0.24434193],\n",
       "       [ 0.32182608],\n",
       "       [-1.51675486],\n",
       "       [-1.23914607],\n",
       "       [-1.35973399],\n",
       "       [ 0.02420483],\n",
       "       [-0.20824763],\n",
       "       [-0.40170145],\n",
       "       [-1.04517912],\n",
       "       [-1.21041023],\n",
       "       [-1.31611709],\n",
       "       [ 0.43317748],\n",
       "       [-1.06519158],\n",
       "       [-1.94317428],\n",
       "       [-0.32062531],\n",
       "       [-0.33140125],\n",
       "       [-0.46943594],\n",
       "       [-0.47097536],\n",
       "       [ 0.13658251],\n",
       "       [-0.45455488],\n",
       "       [-1.48955844],\n",
       "       [-0.01633324],\n",
       "       [ 0.02523111],\n",
       "       [-0.29599459],\n",
       "       [-0.23441778],\n",
       "       [-2.08377466],\n",
       "       [-0.60644434],\n",
       "       [-0.64287729],\n",
       "       [-1.68711737],\n",
       "       [-0.55872231],\n",
       "       [-1.79898191],\n",
       "       [-1.70866926],\n",
       "       [-0.37193932],\n",
       "       [ 0.14017449],\n",
       "       [ 0.21817178],\n",
       "       [ 0.41573072],\n",
       "       [ 0.17609429],\n",
       "       [-0.09433053],\n",
       "       [-0.07688377],\n",
       "       [-0.43659497],\n",
       "       [-0.14718396],\n",
       "       [-1.03696888],\n",
       "       [-1.18013496],\n",
       "       [ 0.15300299],\n",
       "       [-0.5854056 ],\n",
       "       [-0.44737091],\n",
       "       [ 0.21252724],\n",
       "       [-1.53471476],\n",
       "       [-0.22877324],\n",
       "       [-0.95384018],\n",
       "       [-1.9831992 ],\n",
       "       [-1.20989709],\n",
       "       [-1.25505342],\n",
       "       [ 0.15197671],\n",
       "       [-0.05687131],\n",
       "       [-1.56088491],\n",
       "       [-0.4971455 ],\n",
       "       [-1.78923225],\n",
       "       [ 0.72002279],\n",
       "       [-0.89944733],\n",
       "       [-0.42530589],\n",
       "       [-0.28316609],\n",
       "       [-0.18926145],\n",
       "       [-0.11998754],\n",
       "       [-1.12368955],\n",
       "       [ 0.03138879],\n",
       "       [-1.3751282 ],\n",
       "       [-1.3725625 ],\n",
       "       [-1.32330105],\n",
       "       [-0.5956684 ],\n",
       "       [-1.06108646],\n",
       "       [-1.02414038],\n",
       "       [-1.38385158],\n",
       "       [-1.55010897],\n",
       "       [-0.0029916 ],\n",
       "       [ 0.30386618],\n",
       "       [-0.19644541],\n",
       "       [ 0.0801371 ],\n",
       "       [-0.36321594],\n",
       "       [-0.42735845],\n",
       "       [-1.44029699],\n",
       "       [-0.15436792],\n",
       "       [ 0.32182608],\n",
       "       [-1.40899544],\n",
       "       [ 0.05294067],\n",
       "       [-0.55564347],\n",
       "       [ 0.09604444],\n",
       "       [-1.03491632],\n",
       "       [ 0.12939855],\n",
       "       [-1.80154761],\n",
       "       [-0.98822057],\n",
       "       [ 1.34451428],\n",
       "       [ 0.76158714],\n",
       "       [ 1.27575351],\n",
       "       [ 1.65291147],\n",
       "       [ 1.54309949],\n",
       "       [ 0.55940994],\n",
       "       [ 1.28601631],\n",
       "       [ 1.12540346],\n",
       "       [ 0.39417883],\n",
       "       [-0.7927142 ],\n",
       "       [ 1.21982124],\n",
       "       [ 1.52308703],\n",
       "       [ 1.46048394],\n",
       "       [ 1.42712983],\n",
       "       [ 1.21366356],\n",
       "       [ 0.76979738],\n",
       "       [-0.92510434],\n",
       "       [ 1.27113525],\n",
       "       [ 0.58352753],\n",
       "       [-1.22015989],\n",
       "       [ 0.87447796],\n",
       "       [ 1.50717969],\n",
       "       [ 0.09245246],\n",
       "       [ 1.51385051],\n",
       "       [ 0.61893419],\n",
       "       [ 1.66317427],\n",
       "       [ 0.61277651],\n",
       "       [ 1.88023253],\n",
       "       [-0.44531835],\n",
       "       [ 0.17865999],\n",
       "       [ 1.17312549],\n",
       "       [ 1.73347447],\n",
       "       [-0.44583149],\n",
       "       [ 0.76671854],\n",
       "       [ 0.79134926],\n",
       "       [-0.65570579],\n",
       "       [-0.06405527],\n",
       "       [ 0.64048607],\n",
       "       [ 0.07551884],\n",
       "       [-0.17335411],\n",
       "       [ 0.35877217],\n",
       "       [ 0.42240154],\n",
       "       [ 1.00840752],\n",
       "       [-0.27341642],\n",
       "       [ 0.56094936],\n",
       "       [ 0.02933623],\n",
       "       [ 1.07716829],\n",
       "       [-1.08982231],\n",
       "       [ 0.97505341],\n",
       "       [ 0.79494124],\n",
       "       [-0.3396115 ],\n",
       "       [ 0.89449042],\n",
       "       [ 1.43174809],\n",
       "       [ 1.55644113],\n",
       "       [ 0.84215013],\n",
       "       [ 0.73182501],\n",
       "       [ 0.3054056 ],\n",
       "       [ 1.39788085],\n",
       "       [ 1.05305071],\n",
       "       [-0.59464212],\n",
       "       [ 1.30756819],\n",
       "       [ 1.07716829],\n",
       "       [-0.29240261],\n",
       "       [ 0.51476675],\n",
       "       [ 0.69539206],\n",
       "       [ 0.04780927],\n",
       "       [ 0.08065024],\n",
       "       [-1.29046008],\n",
       "       [ 1.06382665],\n",
       "       [ 1.34297486],\n",
       "       [ 0.71848337],\n",
       "       [ 1.11257496],\n",
       "       [-0.53152589],\n",
       "       [ 0.48910975],\n",
       "       [ 0.97813225],\n",
       "       [ 1.21571612],\n",
       "       [ 0.70719429],\n",
       "       [ 0.62663129],\n",
       "       [ 2.1665647 ],\n",
       "       [ 1.43585321],\n",
       "       [ 0.51784559],\n",
       "       [ 1.43380065],\n",
       "       [ 1.52360017],\n",
       "       [ 1.51077167],\n",
       "       [ 0.93400221],\n",
       "       [ 0.45318994],\n",
       "       [-0.29856029],\n",
       "       [-0.54743323],\n",
       "       [ 1.80480094],\n",
       "       [ 0.79596752],\n",
       "       [-0.37501816],\n",
       "       [ 0.3156684 ]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "ss_y = StandardScaler()\n",
    "\n",
    "# y_train = ss_y.fit_transform(y_train_data.values.reshape(-1, 1))\n",
    "# y_test = ss_y.transform(y_test_data.values.reshape(-1, 1))\n",
    "\n",
    "y_train = ss_y.fit_transform(y_train_data.reshape(-1, 1))\n",
    "y_test = ss_y.transform(y_test_data.reshape(-1, 1))\n",
    "y_test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.1 尝试缺省参数的线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5520146213141126\n",
      "0.5933668372748685\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "lr = LinearRegression()\n",
    "lr.fit(X_train, y_train)\n",
    "\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "print(r2_score(y_test, y_test_pred_lr))\n",
    "print(r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>temp</td>\n",
       "      <td>[0.6491598991850696]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>season4</td>\n",
       "      <td>[0.29287402631019066]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weathersit2</td>\n",
       "      <td>[0.2262039856082455]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mnth3</td>\n",
       "      <td>[0.20966054794430272]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth9</td>\n",
       "      <td>[0.18740030849565148]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth10</td>\n",
       "      <td>[0.10319152660673472]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mnth1</td>\n",
       "      <td>[0.09233689060848854]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mnth2</td>\n",
       "      <td>[0.08730485008075112]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>weathersit1</td>\n",
       "      <td>[0.08560332683011293]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth5</td>\n",
       "      <td>[0.04097952971939683]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>weekday2</td>\n",
       "      <td>[0.03938277989295206]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season2</td>\n",
       "      <td>[0.035874234822476696]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>holiday0</td>\n",
       "      <td>[0.007022919747979427]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday5</td>\n",
       "      <td>[0.00017981516733689025]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday3</td>\n",
       "      <td>[-0.0064040642286213945]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday4</td>\n",
       "      <td>[-0.016739012963892304]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth12</td>\n",
       "      <td>[-0.03101168453088125]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>weekday1</td>\n",
       "      <td>[-0.07877331505563276]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>weekday0</td>\n",
       "      <td>[-0.08188192468668393]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-0.13796080748719936]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mnth4</td>\n",
       "      <td>[-0.13848473901193317]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth11</td>\n",
       "      <td>[-0.14664176851690458]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>weekday6</td>\n",
       "      <td>[-0.16480662682326905]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>season3</td>\n",
       "      <td>[-0.1662157235454743]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth8</td>\n",
       "      <td>[-0.17512287396053533]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth6</td>\n",
       "      <td>[-0.17934983400302854]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-0.21441716031585797]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>holiday1</td>\n",
       "      <td>[-0.3160652684458364]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth7</td>\n",
       "      <td>[-0.35930510212984185]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>season1</td>\n",
       "      <td>[-0.47157488628493954]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>weathersit3</td>\n",
       "      <td>[-0.620849661136214]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        columns                      coef\n",
       "28         temp      [0.6491598991850696]\n",
       "3       season4     [0.29287402631019066]\n",
       "25  weathersit2      [0.2262039856082455]\n",
       "6         mnth3     [0.20966054794430272]\n",
       "12        mnth9     [0.18740030849565148]\n",
       "13       mnth10     [0.10319152660673472]\n",
       "4         mnth1     [0.09233689060848854]\n",
       "5         mnth2     [0.08730485008075112]\n",
       "26  weathersit1     [0.08560332683011293]\n",
       "8         mnth5     [0.04097952971939683]\n",
       "21     weekday2     [0.03938277989295206]\n",
       "1       season2    [0.035874234822476696]\n",
       "16     holiday0    [0.007022919747979427]\n",
       "24     weekday5  [0.00017981516733689025]\n",
       "22     weekday3  [-0.0064040642286213945]\n",
       "23     weekday4   [-0.016739012963892304]\n",
       "15       mnth12    [-0.03101168453088125]\n",
       "20     weekday1    [-0.07877331505563276]\n",
       "19     weekday0    [-0.08188192468668393]\n",
       "30    windspeed    [-0.13796080748719936]\n",
       "7         mnth4    [-0.13848473901193317]\n",
       "14       mnth11    [-0.14664176851690458]\n",
       "18     weekday6    [-0.16480662682326905]\n",
       "2       season3     [-0.1662157235454743]\n",
       "11        mnth8    [-0.17512287396053533]\n",
       "9         mnth6    [-0.17934983400302854]\n",
       "29          hum    [-0.21441716031585797]\n",
       "17     holiday1     [-0.3160652684458364]\n",
       "10        mnth7    [-0.35930510212984185]\n",
       "0       season1    [-0.47157488628493954]\n",
       "27  weathersit3      [-0.620849661136214]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colu = ['season1', 'season2', 'season3', 'season4', \n",
    "           'mnth1', 'mnth2', 'mnth3', 'mnth4', 'mnth5', 'mnth6', 'mnth7', 'mnth8', 'mnth9', 'mnth10', 'mnth11', 'mnth12',\n",
    "          'holiday0', 'holiday1',\n",
    "          'weekday6', 'weekday0', 'weekday1', 'weekday2', 'weekday3', 'weekday4', 'weekday5',\n",
    "          'weathersit2', 'weathersit1', 'weathersit3',\n",
    "          'temp', 'hum', 'windspeed']  #'casual', 'registered'\n",
    "fs = pd.DataFrame({\"columns\": list(colu), \"coef\": list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'], ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAekAAAFsCAYAAADos0H8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHX9JREFUeJzt3XuUXWWZ5/HvQ1IkNoFAIIGQoAkKyj3BAhMRjEaFNkjo1So4XMIigIL2Qh0VkFEira0MDI4M6HTaCyAoAVqE1mYaJpJGGAETDNegiUjaQEyKKJeACAnP/HF20kWlKnVSdSrnrarvZ61aZ9/OPs97dqp+5937PTuRmUiSpPJs0+wCJElS5wxpSZIKZUhLklQoQ1qSpEIZ0pIkFcqQliSpUIa0Br2IeCQipjW7jmaKiL+JiN9HxNqImLwVX3dtROzZxbpTIuKuBr3OExHxnkbsS9qaDGkNaJ39ce74xz8z98vMBd3sZ0JEZEQM7aNSm+0S4BOZOSIzf9VxZdX2F6pQfTIiLo2IIb190er1Hu/tfqSBypCWClBA+L8BeKSbbQ7KzBHAO4HjgFP7vCppkDOkNei1721HxKERsTAinouIVRFxabXZndXjM1VvcmpEbBMR/y0ilkfE6oi4OiJG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c7ufvuuzfu75RTTuHGG2/cOD9ixAgA1q5dy/Tp0zn44IM54IADuPnmmzepbeXKlRxxxBFMmjSJ/fffn5///Od1tWnBggUcffTRAMyZM4dTTz2VadOmseeee74mvK+55hoOPfRQJk2axEc/+tGNwXvmmWfS2trKfvvtt/E9gNotry+88ELe8Y53cMMNN9RVS7225HT3uzLz6Xbz5wLzM/NrEXFuNX9OQ6uTJG1169evZ/78+cyePRuA2267jaVLl3LfffeRmRxzzDHceeedtLW1sfvuu/PTn/4UgGefffY1+1m5ciUXXHABixYtYuTIkbzrXe9i8uTJm33t4cOHc9NNN7HDDjvw9NNPM2XKFI455pjXDML6wQ9+wJFHHsn555/P+vXrefHFF3vUzscee4w77riD559/nje/+c2ceeaZLFu2jHnz5nH33XfT0tLCWWedxbXXXsvJJ5/MV77yFUaNGsX69euZPn06Dz74IAceeODGuu+6664e1bE5vbkmPROYVk1fBSzAkJakfuvPf/4zkyZN4oknnuCtb30r733ve4FaSN92220bA3bt2rUsXbqUww8/nM985jOcc845HH300Rx++OGv2d+9997LtGnTGD269h8+HXfccfzmN7/ZbA2Zyec//3nuvPNOttlmG5588klWrVrFbrvttnGbQw45hFNPPZVXXnmFY489lkmTJvWovTNmzGDYsGEMGzaMMWPGsGrVKubPn8+iRYs45JBDNr4nY8aMAeD6669n7ty5rFu3jpUrV/Loo49uDOnjjjuuRzV0p95r0gncFhGLIuKMatmumbkSoHoc0xcFSpK2jg3XpJcvX87LL7+88Zp0ZnLeeeexePFiFi9ezLJly5g9ezZ77703ixYt4oADDuC8887jwgsv3GSfXX0NaejQobz66qsb9//yyy8DcO2119LW1saiRYtYvHgxu+666ybfNT7iiCO48847GTduHCeddBJXX311j9o7bNiwjdNDhgxh3bp1ZCazZs3a2NZf//rXzJkzh9/97ndccsklzJ8/nwcffJAZM2a8pq7tttuuRzV0p96e9GGZ+VREjAFuj4jH6n2BKtTPAHj961/fgxKl/qPeEbmNHrkrNdLIkSO57LLLmDlzJmeeeSZHHnkkX/j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Pz5mqb84F1wLWd7aKTZUV8Z6yettXhsMx8KiLGALdHxGPVJ86maUC7+uUx24LdvL46ZnsCP4uIhzLzt42psGHqOQbFHqdu1FP3vwA/zMy/RMTHqJ0xeHefV9b3+usxq8f91O5nvTYi3g/8GNiryTXVLSJGAP8MfDIzn+u4upOn1H3c+jSkM/M9m1sfEbOAo4HpWZ2876DYW49217Y69/FU9bg6Im6idiqvqSHdgHb1y2MWEasiYmxmrqxORa3uYh8bjtnjEbGA2ifn0kK6nmOwYZsVETEUGEn/OB3Zbdsyc0272X+iNuZlICj2d6u32gdbZv5rRHwzInbJzOL/842IaKEW0Ndm5o862aRXx62Zo7uPAs4BjsnMF7vYbMDeejQitouI7TdMUxtI1+nIx36mvx6zW4BZ1fQsYJOzBhGxU0QMq6Z3AQ4DHt1qFdavnmPQvr0fBH7WxQfl0nTbtg7X+46hdp1wILgFOLkaLTwFeHbDJZr+LiJ22zAmIiIOpZZNazb/rOarav4OsCQzL+1is94dtyaOiltG7Tz94upnw0jT3YF/7TAy7jfUeivnN6veLWzb31D79PQXYBXwbx3bRm106gPVzyP9oW31tKsfH7OdgfnA0upxVLW8Ffh2Nf124KHqmD0EzG523ZtpzybHALiQ2odigOHADdXv4X3Ans2uuYFt+2r1O/UAcAfwlmbXXGe7fgisBF6pfs9mAx8DPlatD+CKqt0PsZlvjpT2U0fbPtHumN0DvL3ZNdfZrndQO3X9YLsse38jj5u3BZUkqVCljO6WJEkdGNKSJBXKkJYkqVCGtCRJhTKkJUkqlCEtSVKhDGlJkgr1/wGKfqwPfeAi8wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "f, ax = plt.subplots(figsize=(7, 5))\n",
    "f.tight_layout()\n",
    "ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5); \n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 正则化的线性回归（L2正则 --> 岭回归）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5520146213141126\n",
      "0.5933668372748685\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import RidgeCV\n",
    "\n",
    "alphas = [0.01, 0.1, 1, 10, 100]\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)\n",
    "ridge.fit(X_train, y_train)\n",
    "\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "\n",
    "print(r2_score(y_test, y_test_pred_lr))\n",
    "print(r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD8CAYAAAB3u9PLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xl0nfV95/H3V5JlWd4tybY2WzYYMHjFMjgBEhICMYtZJrahwyHNNDkMmcmkc9q0TSdtegZOpymdE9pMaXNopp3ktA2WIYBt7BAgMSQti66x5RXjnavFq7C8yLa27/xxH4lrcSVd2bqrPq9zdHTv8/zu83yfx7I+epb7vebuiIiI5KS6ABERSQ8KBBERARQIIiISUCCIiAigQBARkYACQUREAAWCiIgEFAgiIgIoEEREJJCX6gIGo7i42KuqqlJdhohIRtm0adNxdy8ZaFxGBUJVVRWhUCjVZYiIZBQzOxTPOJ0yEhERQIEgIiIBBYKIiAAKBBERCSgQREQEUCCIiEhAgSAiIoACQUQkrdWFT/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pIoDvzCzTWb2aKqLCaRif01x9yaI/IcBJvcxrsDMQmb2tpklKjTi2f6eMcEfJC1AUYLqGUxdAF8KTjM8Z2bp8An36fz/71NmVmdmG8zsumSvPDjVuBB4p9eshO6zjP5MZTN7DZgaY9Z33P2lYMx3gA7gX2ItIsa0y77tKp664nCTuzea2WTgVTN7P/jLJpV1JX1/DWIx04L9NRP4pZltc/d9l1tbL/Fsf0L20QDiWeda4KfufsHMHiNyFPP5BNc1kFTsq3i8R6TVwxkzuwt4EZiVrJWb2RjgeeC/u/up3rNjvGTI9llGB4K7f6G/+Wb228A9wG0enIDrpR6I/kupAmhMdF1xLqMx+H7UzF4gclrgsgJhCOpK+v4ysyNmVuruTcGh8dE+ltG9v/ab2UYif10NdSDEs/3dY+rNLA8YT+JPTwxYl7ufiHr6D0Suq6VaQn6eLlf0L2F3X29mf2dmxe6e8B5HZjaCSBj8i7v/LMaQhO6zrD1lZGZLgT8C7nX31j6G1QKzzGyGmeUTuQiYsDtU4mVmo81sbPdjIhfIY94RkWSp2F9rgN8OHv828IkjGTObaGYjg8fFwE3AzgTUEs/2R9e7HPhlH3+MJLWuXueZ7yVyfjrV1gBfDu6cWQK0dJ8eTCUzm9p93cfMbiDye/JE/68akvUa8H+BXe7+/T6GJXafJftKerK+gL1EzrVtCb667/woA9ZHjbuLyNX8fUROnSS6rgeIpPwF4AjwSu+6iNwtUhd87UiXulK0v4qA14E9wfdJwfRq4EfB408D24L9tQ34agLr+cT2A48T+cMDoABYHfz8vQvMTPQ+irOuvwh+luqAXwHXJKGmnwJNQHvws/VV4DHgsWC+AU8HNW+jn7vuklzXN6L21dvAp5NU181ETv9sjfq9dVcy95neqSwiIkAWnzISEZHBUSCIiAigQBARkYACQUREAAWCiIgEFAgiIgIoEEREJKBAEBERAP4/uLfuzr+FSUgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n"
     ]
    }
   ],
   "source": [
    "mse_mean = np.mean(ridge.cv_values_, axis=0)\n",
    "plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas), 1))\n",
    "plt.show()\n",
    "\n",
    "print(ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>temp</td>\n",
       "      <td>[0.6491598991850696]</td>\n",
       "      <td>[0.5908922233233938]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>season4</td>\n",
       "      <td>[0.29287402631019066]</td>\n",
       "      <td>[0.26396942919044136]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weathersit2</td>\n",
       "      <td>[0.2262039856082455]</td>\n",
       "      <td>[0.16339987470578202]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mnth3</td>\n",
       "      <td>[0.20966054794430272]</td>\n",
       "      <td>[0.10851992304489991]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth9</td>\n",
       "      <td>[0.18740030849565148]</td>\n",
       "      <td>[0.20550289620096587]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth10</td>\n",
       "      <td>[0.10319152660673472]</td>\n",
       "      <td>[0.13340892616354827]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mnth1</td>\n",
       "      <td>[0.09233689060848854]</td>\n",
       "      <td>[-0.06095825775863074]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mnth2</td>\n",
       "      <td>[0.08730485008075112]</td>\n",
       "      <td>[-0.04524323948033077]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>weathersit1</td>\n",
       "      <td>[0.08560332683011293]</td>\n",
       "      <td>[0.03248136237903674]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth5</td>\n",
       "      <td>[0.04097952971939683]</td>\n",
       "      <td>[0.0842540021393951]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>weekday2</td>\n",
       "      <td>[0.03938277989295206]</td>\n",
       "      <td>[0.045314131721698826]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season2</td>\n",
       "      <td>[0.035874234822476696]</td>\n",
       "      <td>[0.048300887372455185]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>holiday0</td>\n",
       "      <td>[0.007022919747979427]</td>\n",
       "      <td>[0.005054003795696135]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday5</td>\n",
       "      <td>[0.00017981516733689025]</td>\n",
       "      <td>[0.008226586120347074]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday3</td>\n",
       "      <td>[-0.0064040642286213945]</td>\n",
       "      <td>[0.01100665019075936]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday4</td>\n",
       "      <td>[-0.016739012963892304]</td>\n",
       "      <td>[-0.0011054226207990736]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth12</td>\n",
       "      <td>[-0.03101168453088125]</td>\n",
       "      <td>[-0.0779927778677447]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>weekday1</td>\n",
       "      <td>[-0.07877331505563276]</td>\n",
       "      <td>[-0.05310368179681546]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>weekday0</td>\n",
       "      <td>[-0.08188192468668393]</td>\n",
       "      <td>[-0.07997344711176393]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-0.13796080748719936]</td>\n",
       "      <td>[-0.14570202335186064]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mnth4</td>\n",
       "      <td>[-0.13848473901193317]</td>\n",
       "      <td>[-0.10133263645931968]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth11</td>\n",
       "      <td>[-0.14664176851690458]</td>\n",
       "      <td>[-0.11278560494304989]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>weekday6</td>\n",
       "      <td>[-0.16480662682326905]</td>\n",
       "      <td>[-0.12799458986912027]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>season3</td>\n",
       "      <td>[-0.1662157235454743]</td>\n",
       "      <td>[-0.15698773420174653]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth8</td>\n",
       "      <td>[-0.17512287396053533]</td>\n",
       "      <td>[-0.059774545419565395]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth6</td>\n",
       "      <td>[-0.17934983400302854]</td>\n",
       "      <td>[-0.0667306316844345]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-0.21441716031585797]</td>\n",
       "      <td>[-0.21167843440875955]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>holiday1</td>\n",
       "      <td>[-0.3160652684458364]</td>\n",
       "      <td>[-0.20268377716138222]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth7</td>\n",
       "      <td>[-0.35930510212984185]</td>\n",
       "      <td>[-0.20449782730142643]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>season1</td>\n",
       "      <td>[-0.47157488628493954]</td>\n",
       "      <td>[-0.3529123557268418]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>weathersit3</td>\n",
       "      <td>[-0.620849661136214]</td>\n",
       "      <td>[-0.39351101045050985]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        columns                   coef_lr                coef_ridge\n",
       "28         temp      [0.6491598991850696]      [0.5908922233233938]\n",
       "3       season4     [0.29287402631019066]     [0.26396942919044136]\n",
       "25  weathersit2      [0.2262039856082455]     [0.16339987470578202]\n",
       "6         mnth3     [0.20966054794430272]     [0.10851992304489991]\n",
       "12        mnth9     [0.18740030849565148]     [0.20550289620096587]\n",
       "13       mnth10     [0.10319152660673472]     [0.13340892616354827]\n",
       "4         mnth1     [0.09233689060848854]    [-0.06095825775863074]\n",
       "5         mnth2     [0.08730485008075112]    [-0.04524323948033077]\n",
       "26  weathersit1     [0.08560332683011293]     [0.03248136237903674]\n",
       "8         mnth5     [0.04097952971939683]      [0.0842540021393951]\n",
       "21     weekday2     [0.03938277989295206]    [0.045314131721698826]\n",
       "1       season2    [0.035874234822476696]    [0.048300887372455185]\n",
       "16     holiday0    [0.007022919747979427]    [0.005054003795696135]\n",
       "24     weekday5  [0.00017981516733689025]    [0.008226586120347074]\n",
       "22     weekday3  [-0.0064040642286213945]     [0.01100665019075936]\n",
       "23     weekday4   [-0.016739012963892304]  [-0.0011054226207990736]\n",
       "15       mnth12    [-0.03101168453088125]     [-0.0779927778677447]\n",
       "20     weekday1    [-0.07877331505563276]    [-0.05310368179681546]\n",
       "19     weekday0    [-0.08188192468668393]    [-0.07997344711176393]\n",
       "30    windspeed    [-0.13796080748719936]    [-0.14570202335186064]\n",
       "7         mnth4    [-0.13848473901193317]    [-0.10133263645931968]\n",
       "14       mnth11    [-0.14664176851690458]    [-0.11278560494304989]\n",
       "18     weekday6    [-0.16480662682326905]    [-0.12799458986912027]\n",
       "2       season3     [-0.1662157235454743]    [-0.15698773420174653]\n",
       "11        mnth8    [-0.17512287396053533]   [-0.059774545419565395]\n",
       "9         mnth6    [-0.17934983400302854]     [-0.0667306316844345]\n",
       "29          hum    [-0.21441716031585797]    [-0.21167843440875955]\n",
       "17     holiday1     [-0.3160652684458364]    [-0.20268377716138222]\n",
       "10        mnth7    [-0.35930510212984185]    [-0.20449782730142643]\n",
       "0       season1    [-0.47157488628493954]     [-0.3529123557268418]\n",
       "27  weathersit3      [-0.620849661136214]    [-0.39351101045050985]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs = pd.DataFrame({\"columns\": list(colu), \"coef_lr\": list((lr.coef_.T)), \"coef_ridge\": list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'], ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.3 正则化的线性回归（L1正则 --> Lasso）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.5509938507004735\n",
      "The r2 score of LassoCV on train is 0.5914806526011991\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/admin/Documents/Anaconda5.2/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:1094: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LassoCV\n",
    "lasso = LassoCV()  \n",
    "lasso.fit(X_train, y_train)  \n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "print('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))\n",
    "print('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHlRJREFUeJzt3XmUXGW57/HvUz0l6Qw9Zuqk05kgCWFM34QQkVkCKqAsliAHUdE4get4j3cJci54VM6Ro16Hu5xY6lUUQQWHICijgBCGdEJC5nnqIT2kO92doaeq5/7R1aEN3enqpLp2VfXvs1Kratd+s/ezs9O/2v3ut/Y2d0dERNJLKOgCREQk/hTuIiJpSOEuIpKGFO4iImlI4S4ikoYU7iIiaUjhLiKShhTuIiJpSOEuIpKGMoNacVFRkZeVlQW1ehGRlLRq1aoGdy8eqF1g4V5WVkZFRUVQqxcRSUlmtieWduqWERFJQwp3EZE0pHAXEUlDCncRkTSkcBcRSUMKdxGRNKRwFxFJQwp3EZEE+t6z23h5W8OQr0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FvA4UBJ9vxz4aa92Hwe2Rx8fC7rufrZlO939nWuijx9H358MPBl9PYPuEQxrgQ10/7odeO0nsy3R6auBrXQfTSXdtgAfoPtoth2oBZ5K4X0y4Lakwj6J1lgIPAdsiz4XRN8/9nMPXACsi+6XdcBtQdd9Kg99Q1VEJA0Nu24ZEZHhQOEuIpKGFO4iImlI4S4ikoYU7iIiaUjhLiKShhTuIiJpSOEuIpKG/j8r3JuU46ppXQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0019995639564519786\n"
     ]
    }
   ],
   "source": [
    "mses = np.mean(lasso.mse_path_, axis=1)\n",
    "plt.plot(np.log10(lasso.alphas_), mses)\n",
    "plt.show()\n",
    "\n",
    "print(lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>temp</td>\n",
       "      <td>[0.6491598991850696]</td>\n",
       "      <td>6.096096e-01</td>\n",
       "      <td>6.096096e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>season4</td>\n",
       "      <td>[0.29287402631019066]</td>\n",
       "      <td>3.002661e-01</td>\n",
       "      <td>3.002661e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weathersit2</td>\n",
       "      <td>[0.2262039856082455]</td>\n",
       "      <td>1.369949e-01</td>\n",
       "      <td>1.369949e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mnth3</td>\n",
       "      <td>[0.20966054794430272]</td>\n",
       "      <td>1.909388e-01</td>\n",
       "      <td>1.909388e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth9</td>\n",
       "      <td>[0.18740030849565148]</td>\n",
       "      <td>2.910708e-01</td>\n",
       "      <td>2.910708e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth10</td>\n",
       "      <td>[0.10319152660673472]</td>\n",
       "      <td>1.764621e-01</td>\n",
       "      <td>1.764621e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mnth1</td>\n",
       "      <td>[0.09233689060848854]</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mnth2</td>\n",
       "      <td>[0.08730485008075112]</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>weathersit1</td>\n",
       "      <td>[0.08560332683011293]</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth5</td>\n",
       "      <td>[0.04097952971939683]</td>\n",
       "      <td>1.538192e-01</td>\n",
       "      <td>1.538192e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>weekday2</td>\n",
       "      <td>[0.03938277989295206]</td>\n",
       "      <td>4.626041e-02</td>\n",
       "      <td>4.626041e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season2</td>\n",
       "      <td>[0.035874234822476696]</td>\n",
       "      <td>3.048344e-02</td>\n",
       "      <td>3.048344e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>holiday0</td>\n",
       "      <td>[0.007022919747979427]</td>\n",
       "      <td>2.710068e-01</td>\n",
       "      <td>2.710068e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday5</td>\n",
       "      <td>[0.00017981516733689025]</td>\n",
       "      <td>3.366032e-04</td>\n",
       "      <td>3.366032e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday3</td>\n",
       "      <td>[-0.0064040642286213945]</td>\n",
       "      <td>1.398644e-03</td>\n",
       "      <td>1.398644e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday4</td>\n",
       "      <td>[-0.016739012963892304]</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth12</td>\n",
       "      <td>[-0.03101168453088125]</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>weekday1</td>\n",
       "      <td>[-0.07877331505563276]</td>\n",
       "      <td>-3.985586e-02</td>\n",
       "      <td>-3.985586e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>weekday0</td>\n",
       "      <td>[-0.08188192468668393]</td>\n",
       "      <td>-5.978425e-02</td>\n",
       "      <td>-5.978425e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-0.13796080748719936]</td>\n",
       "      <td>-1.386642e-01</td>\n",
       "      <td>-1.386642e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mnth4</td>\n",
       "      <td>[-0.13848473901193317]</td>\n",
       "      <td>-3.978113e-03</td>\n",
       "      <td>-3.978113e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth11</td>\n",
       "      <td>[-0.14664176851690458]</td>\n",
       "      <td>-5.059006e-02</td>\n",
       "      <td>-5.059006e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>weekday6</td>\n",
       "      <td>[-0.16480662682326905]</td>\n",
       "      <td>-1.326532e-01</td>\n",
       "      <td>-1.326532e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>season3</td>\n",
       "      <td>[-0.1662157235454743]</td>\n",
       "      <td>-1.672531e-01</td>\n",
       "      <td>-1.672531e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth8</td>\n",
       "      <td>[-0.17512287396053533]</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth6</td>\n",
       "      <td>[-0.17934983400302854]</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-0.21441716031585797]</td>\n",
       "      <td>-2.085936e-01</td>\n",
       "      <td>-2.085936e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>holiday1</td>\n",
       "      <td>[-0.3160652684458364]</td>\n",
       "      <td>-3.650290e-14</td>\n",
       "      <td>-3.650290e-14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth7</td>\n",
       "      <td>[-0.35930510212984185]</td>\n",
       "      <td>-1.678862e-01</td>\n",
       "      <td>-1.678862e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>season1</td>\n",
       "      <td>[-0.47157488628493954]</td>\n",
       "      <td>-3.371850e-01</td>\n",
       "      <td>-3.371850e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>weathersit3</td>\n",
       "      <td>[-0.620849661136214]</td>\n",
       "      <td>-6.390567e-01</td>\n",
       "      <td>-6.390567e-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        columns                   coef_lr    coef_ridge    coef_lasso\n",
       "28         temp      [0.6491598991850696]  6.096096e-01  6.096096e-01\n",
       "3       season4     [0.29287402631019066]  3.002661e-01  3.002661e-01\n",
       "25  weathersit2      [0.2262039856082455]  1.369949e-01  1.369949e-01\n",
       "6         mnth3     [0.20966054794430272]  1.909388e-01  1.909388e-01\n",
       "12        mnth9     [0.18740030849565148]  2.910708e-01  2.910708e-01\n",
       "13       mnth10     [0.10319152660673472]  1.764621e-01  1.764621e-01\n",
       "4         mnth1     [0.09233689060848854]  0.000000e+00  0.000000e+00\n",
       "5         mnth2     [0.08730485008075112]  0.000000e+00  0.000000e+00\n",
       "26  weathersit1     [0.08560332683011293] -0.000000e+00 -0.000000e+00\n",
       "8         mnth5     [0.04097952971939683]  1.538192e-01  1.538192e-01\n",
       "21     weekday2     [0.03938277989295206]  4.626041e-02  4.626041e-02\n",
       "1       season2    [0.035874234822476696]  3.048344e-02  3.048344e-02\n",
       "16     holiday0    [0.007022919747979427]  2.710068e-01  2.710068e-01\n",
       "24     weekday5  [0.00017981516733689025]  3.366032e-04  3.366032e-04\n",
       "22     weekday3  [-0.0064040642286213945]  1.398644e-03  1.398644e-03\n",
       "23     weekday4   [-0.016739012963892304] -0.000000e+00 -0.000000e+00\n",
       "15       mnth12    [-0.03101168453088125] -0.000000e+00 -0.000000e+00\n",
       "20     weekday1    [-0.07877331505563276] -3.985586e-02 -3.985586e-02\n",
       "19     weekday0    [-0.08188192468668393] -5.978425e-02 -5.978425e-02\n",
       "30    windspeed    [-0.13796080748719936] -1.386642e-01 -1.386642e-01\n",
       "7         mnth4    [-0.13848473901193317] -3.978113e-03 -3.978113e-03\n",
       "14       mnth11    [-0.14664176851690458] -5.059006e-02 -5.059006e-02\n",
       "18     weekday6    [-0.16480662682326905] -1.326532e-01 -1.326532e-01\n",
       "2       season3     [-0.1662157235454743] -1.672531e-01 -1.672531e-01\n",
       "11        mnth8    [-0.17512287396053533] -0.000000e+00 -0.000000e+00\n",
       "9         mnth6    [-0.17934983400302854] -0.000000e+00 -0.000000e+00\n",
       "29          hum    [-0.21441716031585797] -2.085936e-01 -2.085936e-01\n",
       "17     holiday1     [-0.3160652684458364] -3.650290e-14 -3.650290e-14\n",
       "10        mnth7    [-0.35930510212984185] -1.678862e-01 -1.678862e-01\n",
       "0       season1    [-0.47157488628493954] -3.371850e-01 -3.371850e-01\n",
       "27  weathersit3      [-0.620849661136214] -6.390567e-01 -6.390567e-01"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs = pd.DataFrame({\"columns\": list(colu), \"coef_lr\": list((lr.coef_.T)), \"coef_ridge\": list((lasso.coef_.T)), \"coef_lasso\": list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'], ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上图可以看出，temp气温、season4、mnth9、season1、weathersit3影响自行车租赁的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
